faApp.controller('VarietyProductListCtrl', ['$scope', '$location', '$controller', '$route', '$routeParams', function($scope, $location, $controller, $route, $routeParams) {
    $scope.tabUrl = {};
    $scope.type = $routeParams.t;
    $scope.tabUrlMap = {
        samePro: "#/varietyProduct/samePro?type=" + $scope.type,
        // likePro: "#/varietyProduct/likePro?id=" + $scope.id,
        newPro: "#/varietyProduct/newPro?type=" + $scope.type,
    };
    $scope.changeTab = function(tab) {
        $scope.tabUrl[tab] = $scope.tabUrlMap[tab];
    };
    $scope.changeTab('samePro');
}]);
faApp.controller('SameProductListCtrl', ['$scope', '$location', '$controller', '$route', '$routeParams', "varietyService", function($scope, $location, $controller, $route, $routeParams, varietyService) {
    $controller('BaseListCtrl', {
        $scope: $scope
    });
    $scope.config.pType = $routeParams.type;
    $scope.config.pageSize = 10;
    $scope.config.productSource = "1caifu"; //设置壹财富来源
    $scope.$watch('config', function() {
        varietyService.yesBindingProList($scope.config).then(function(json) {
            $scope.list = json.bindProjectList;
            $scope.pager = json.page;


        })
    }, true);
    //标记为不同产品
    $scope.markDiff = function(o) {
        if( confirm('确定为不同产品') ) {            
            var idGroup = {
                id: o,
            }
            varietyService.removeBindingPro(idGroup).then(function(json) {
                if (json.statusCode) {
                    alert("出错了:" + json.statusCode);
                } else {
                    $route.reload();
                }
            })
        }
    }
}]);
faApp.controller('SameProductDetailCtrl', ['$scope', '$timeout','$filter', '$sce', '$location', '$controller', '$route', '$routeParams', 'varietyService', function($scope, $timeout,$filter, $sce, $location, $controller, $route, $routeParams, varietyService) {
    $controller('BaseCtrl', {
        $scope: $scope
    });
    $scope.type = $routeParams.type;
    $scope.proId = {
        'fetchPId': $routeParams.fetchId,
        'pId': $routeParams.pId,
        'bindId': $routeParams.bindId
    }
    $scope.$watch('proId', function() {
        varietyService.getNewProInfo($scope.proId['fetchPId']).then(function(json) {
            $scope.detail = json.fpdto;
            $scope.detail.productAnnual = $sce.trustAsHtml($scope.detail.productAnnual)
        })
        varietyService.getSfaProInfo($scope.proId['pId']).then(function(json) {
            $scope.info = json.project;
            $scope.projectAnnualRevenue = json.projectAnnualRevenueList;
            $scope.projectRabateList = json.projectRabateList;
        })
    }, true);
    $scope.addInfo = [];
    $scope.showAdd = function() {
        // $scope.addOther = true;
        $scope.item = {
            addOther : true,
            pStartAmount: '',
            pEndAmount: '',
            pAnnualRevenueExpect: '',
            pRabateProfit: ''
        }
        $scope.addInfo.push($scope.item);
        console.log($scope.addInfo);
    }
    $scope.infoDelete = function(index) {
        $scope.addInfo.splice(index,1);
        console.log($scope.addInfo);
    }

    //保存功能
    $scope.save = function() {

        $scope.formData = {
            pId: [],
            pRebateType: [],
            pStartAmount: [],
            pEndAmount: [],
            pAnnualRevenueExpect: [],
            pIsAgreement: [],
            pRabateProfit: [],
            pCorePrice: [],
            pCostPrice: [],
            pSellingPrice: [],
            pCostBackPrice: [],
            pCoreBackPrice: [],
            pBackSellingPrice: [],
            pBackRabateProfit: []
        };
        angular.forEach($scope.projectAnnualRevenue, function(o, i) {
            $scope.formData['pId'].push(o['pId']);
            $scope.formData['pRebateType'].push("01");
            $scope.formData['pStartAmount'].push(o['pStartAmount']);
            $scope.formData['pEndAmount'].push(o['pEndAmount']);
            $scope.formData['pAnnualRevenueExpect'].push(o['pAnnualRevenueExpect']);
            $scope.formData['pRabateProfit'].push($scope.projectRabateList[i]['pRabateProfit'] || 0);
            $scope.formData['pCorePrice'].push($scope.projectRabateList[i]['pCorePrice'] || 0);
            $scope.formData['pCostPrice'].push($scope.projectRabateList[i]['pCostPrice'] || 0);
            $scope.formData['pSellingPrice'].push($scope.projectRabateList[i]['pSellingPrice'] || 0);
            $scope.formData['pCostBackPrice'].push($scope.projectRabateList[i]['pCostBackPrice'] || 0);
            $scope.formData['pCoreBackPrice'].push($scope.projectRabateList[i]['pCoreBackPrice'] || 0);
            $scope.formData['pBackSellingPrice'].push($scope.projectRabateList[i]['pBackSellingPrice'] || 0);
            $scope.formData['pBackRabateProfit'].push($scope.projectRabateList[i]['pBackRabateProfit'] || 0);

            $scope.formData['pIsAgreement'].push($scope.projectRabateList[i]['pIsAgreement']);
        });       
        if ($scope.addInfo.length) {
            var isNull = false; 
            $('.addAd .nokong').each(function(){
                if( !$(this).val() ) {
                    isNull = true;
                }
            })
            if ( isNull ) {
                alert('请填写完整表单');
                return;
            } else {
                alert($scope.addInfo.length);
                for (var i = 0;i<$scope.addInfo.length; i++) {                   
                    $scope.formData['pId'].push($routeParams.pId);
                    $scope.formData['pRebateType'].push("01");
                    $scope.formData['pStartAmount'].push($scope.addInfo[i].pStartAmount);
                    $scope.formData['pEndAmount'].push($scope.addInfo[i].pEndAmount);
                    $scope.formData['pAnnualRevenueExpect'].push($scope.addInfo[i].pAnnualRevenueExpect);
                    $scope.formData['pRabateProfit'].push($scope.addInfo[i].pRabateProfit);
                    $scope.formData['pCorePrice'].push(0);
                    $scope.formData['pCostPrice'].push(0);
                    $scope.formData['pSellingPrice'].push(0);
                    $scope.formData['pCostBackPrice'].push(0);
                    $scope.formData['pCoreBackPrice'].push(0);
                    $scope.formData['pBackSellingPrice'].push(0);
                    $scope.formData['pBackRabateProfit'].push(0);
                    $scope.formData['pIsAgreement'].push(0);
                }
            }
        }
        $scope.formData2 = {
            id: $scope.info.id,
            fetchId: $scope.proId.fetchPId,
            pRecruitmentSummary: $scope.info.pRecruitmentSummary
        };
        for (k in $scope.formData) {
            $scope.formData2[k] = $scope.formData[k].join(",");
        }
        varietyService.updataProInfo($scope.formData2).then(function(json) {
            if (json.statusCode) {
                alert("出错了:" + json.statusCode);
            } else {
                alert('修改成功');
                $route.reload();
            }
        });
    };
    //删除功能
    $scope.deleteItem = function(v) {
        if ($scope.projectRabateList.length <= 1) {
            alert("最后一个不能删除");
            return;
        }

        $scope.projectAnnualRevenue.splice(v, 1);
        $scope.projectRabateList.splice(v, 1);

        $timeout(function() {
            $scope.save();
        }, 10);
    };
    //标记为不同产品
    $scope.markDiff = function() {
        if ( confirm('确定为不同产品') ) {        
            var idGroup = {
                id: $scope.proId.bindId
            }
            varietyService.removeBindingPro(idGroup).then(function(json) {
                if (json.statusCode) {
                    alert("出错了:" + json.statusCode);
                } else {
                    $location.path('/varietyProduct/samePro').search({ type : $scope.type });
                }
            })
        }
    }
}]);
// faApp.controller('LikeProductListCtrl', ['$scope', '$location', '$controller', '$route', '$routeParams', function($scope,$location, $controller, $route, $routeParams) {
//     $controller('BaseListCtrl', {
//         $scope: $scope
//     });
//     $scope.$watch('config', function() {
//         console.log($scope.config);
//     }, true);
//     alert(2);
// }]);
faApp.controller('NewProductListCtrl', ['$scope', '$location', '$controller', '$route', '$routeParams', 'varietyService', function($scope, $location, $controller, $route, $routeParams, varietyService) {
    $controller('BaseListCtrl', {
        $scope: $scope
    });
    $scope.config.productSource = "1caifu"; //设置壹财富来源
    $scope.config.pType = $routeParams.type;
    $scope.config.pageSize = 10;
    $scope.$watch('config', function() {
        varietyService.getNewProList($scope.config).then(function(json) {
            $scope.list = json.list;
            $scope.pager = json.fnProductDto; //设置分页

        })
    }, true);
}]);
faApp.controller('NewProductDetailCtrl', ['$scope', '$sce', '$location', '$controller', '$route', '$routeParams', 'varietyService', function($scope, $sce, $location, $controller, $route, $routeParams, varietyService) {

    $scope.myUrl = '#/varietyProduct/add?t=' + $routeParams.type + '&fetid=' + $routeParams.id;
    $scope.NewProductId = $routeParams.id;
    $scope.$watch('NewProductId', function() {
        varietyService.getNewProInfo($scope.NewProductId).then(function(json) {
            $scope.detail = json.fpdto;
            $scope.detail.productAnnual = $sce.trustAsHtml($scope.detail.productAnnual);
        })
    }, true);
    $scope.showEstablishaFormFlag = false;
    $scope.changeStatus = function() {
            $scope.showEstablishaFormFlag = true;
        }
        //请求sfa未绑定产品
    $scope.type = $routeParams.type;
    var proType = $routeParams.type; //信托或者资管类型
    varietyService.noBindingProList(proType).then(function(json) {
            $scope.list = json.noBindProjectList;
        })
        //放弃该产品
    $scope.giveUpThisPro = function(id) {
        if ( confirm('确定放弃该产品') ) {
            varietyService.giveUpFetchingPro(id).then(function(json) {
                if (json.statusCode) {
                    alert('出错了' + json.statusCode)
                } else {
                    $location.path('/varietyProduct/newPro').search({
                        type: $routeParams.type
                    })
                }
            })
        }
    }
        //增加绑定关系
    $scope.addBinding = function() {
        $scope.proId = {
            'fetchPId': $scope.detail.id,
            'pId': $scope.selfPro,
            'associationType': 1
        }
        varietyService.addBindingPro($scope.proId).then(function(json) {
            if (json.statusCode) {
                alert('出错了' + json.statusCode)
            } else {
                $scope.showEstablishaFormFlag = false;
                // $location.url('/varietyProduct/samePro?type='+$routeParams.type);
                parent.location.reload() //iframe 上级页面刷新
            }
        })
    }
}]);
faApp.controller('addsNewProductCtrl', ['$scope', 'varietyService', 'productService', 'reportService', '$location', '$controller', '$route', '$routeParams', function($scope, varietyService, productService, reportService, $location, $controller, $route, $routeParams) {
    $controller('ProductAddCtrl', {
        $scope: $scope
    });
    $scope.product.fetchId = $routeParams.fetid;
    $scope.infoSave = function() {
        if (!$scope.form.$invalid) {
            $scope.selectTjzls = [];
            var flag = false;
            for (var i in $scope.allTjzls) {
                if ($scope.allTjzls[i]) {
                    flag = true;
                    $scope.selectTjzls.push(i);
                }
            }
            if (!flag) {
                alert("请勾选提交资料");
                return;
            }
            $scope.product.id = "";
            $scope.product.pType = $scope.t;
            //$scope.product.pIssuingScale = $scope.product.pIssuingScaleTmp * 100000000;
            $scope.product.pIssuingScale = 0;

            $scope.product.pAllIssuingScale = $scope.product.pAllIssuingScale * 100000000;
            $scope.product.pProgressiveAmount = $scope.product.pProgressiveAmountTmp * 10000;
            $scope.product.pMinimumAmount = $scope.product.pMinimumAmountTmp * 10000;
            if ($scope.t != undefined && $scope.t == "02") {
                $scope.product.pInvestOwnerId && ($scope.product.pInvestOwner = $scope.companysMap[$scope.product.pInvestOwnerId]['cShortName']);
            }
            $scope.product.pInvestOwnerId = $scope.product.pInvestOwnerId || -1;
            if ($scope.product.pInvestOwnerId == -1 && $scope.t == "02") {
                $scope.product.pInvestOwner = "";
            }

            if ($scope.ct && $scope.report.id) {
                $scope.product.copyType = "01";
            }

            $scope.product.tjzls = $scope.selectTjzls.join(",");

            varietyService.addNewPro($scope.product).then(function(json) {
                if (json.statusCode) {
                    alert("出错了:" + json.statusCode);
                } else {
                    alert("产品录入成功!");
                    var e_url = '/product/edit?id='+json.projectDTO.id+'&t='+$scope.t;
                    parent.parent.location.href =  e_url ;//跳转到编辑页面
                }
            });
        } else {
            if ($scope.product.pSummary == undefined || !$scope.product.pSummary) {
                alert("请填写产品简介");
            } else alert("请完整填写表单");
        }
    };
}]);